
use "$project\merger_lbnr.dta", clear
bysort lbnr: egen maxnworker=max(nworker)
drop if maxnworker<3
drop if region==.

merge m:1 cvrnr year using "$home\firmdata_v1.dta", keepus (valueadded fulltimeeq/*
*/  sales vapp start_year) nogen keep(1 3)
gen firmage=year-start_year

bysort cvrnr year: gen nestab=_N

*firm FE
merge m:1 lbnr year using "$project\merger_lbnr", keepus(lbnr_merger target) keep(1 3) nogen 
ren lbnr_merger firmid
merge m:1 firmid using "$project\AKM\firmeffs_annualwage_ma_lbnr_separate.dta", keep(1 3) keepus(fe_1) nogen


*calculate characteristics

gen emp_decile=.
gen wage_decile=.
forv yr=1995(1)2010 {
xtile temp_emp_decile=nworker if year==`yr', n(4)
xtile temp_wage_decile=log_income if year==`yr', n(4)
quietly replace emp_decile=temp_emp_decile if year==`yr'
quietly replace wage_decile=temp_wage_decile if year==`yr'
drop temp_emp_decile temp_wage_decile
}
xtile fe_decile=fe_1, n(10)
*gen emp_decile_lag1=L1.emp_decile
*gen wage_decile_lag1=L1.wage_decile

*estimate propensity score

tsset lbnr year
gen log_nworker=log(nworker)
/*
foreach var in log_nworker ln_hrwage sh_educ_voc sh_educ_high sh_female avg_age avg_exper vapp log_income {
quietly gen `var'_lag1=L1.`var'
}
*/
cap drop target_lead
gen target_lead=F1.target
gen emp_growth=log(nworker/L.nworker)
gen wage_growth=log_income-L.log_income
gen emp_growth_lag1=log(L.nworker/L2.nworker)
gen wage_growth_lag1=L.log_income-L2.log_income
gen wage_growth2=ln_hrwage-L.ln_hrwage
cap drop nmerger
bysort lbnr: egen nmerger=sum(merger)

xtile decile_emp_growth=emp_growth, n(6)
xtile decile_emp_growth_lag=emp_growth_lag1, n(2)
xtile decile_wage_growth=wage_growth, n(4)
xtile decile_wage_growth2=wage_growth2, n(4)

*egen ind36_num=group(ind36)
reghdfe target_lead log_nworker ln_hrwage sh_educ_voc sh_educ_high emp_growth* wage_growth* /*
*/ sh_female avg_age avg_exper vapp fe_1, absorb(i.year#i.ind127)
predict pscore, xb

reghdfe target_lead log_nworker ln_hrwage sh_educ_voc sh_educ_high emp_growth* wage_growth* /*
*/ sh_female avg_age avg_exper fe_1, absorb(i.year#i.ind127)
predict pscore2, xb

*drop establishments that are acquired twice
cap drop ntarget
bysort lbnr: egen ntarget=sum(target)
drop if ntarget>=2

*matching
egen matchinggroup=group(year region ind36 wage_decile emp_decile decile_emp_growth /*
*/ decile_wage_growth decile_emp_growth_lag)
bysort matchinggroup: gen nestab_group=_N

bysort target_lead: gen id=_n
replace id=. if target_lead==0|target_lead==.

*drop if ambiguous or exiting
drop if target_lead==.
drop if matchinggroup==.
drop if target_lead==0&nmerger>0

sort matchinggroup pscore
by matchinggroup: replace id=id[_n-1] if target_lead[_n-1]==1&nmerger==0&id==.&pscore[_n-1]!=.&pscore!=.
by matchinggroup: replace id=id[_n+1] if target_lead[_n+1]==1&nmerger==0&id==.&pscore[_n+1]!=.&pscore!=.
bysort id: egen temp_pscore=max(target_lead*pscore)
gen temp_diff=(pscore-temp_pscore)^2
sort id temp_diff
by id: gen temp_order=_n
replace id=. if temp_order>2
bysort id: gen temp_count=_N
gen matched=1 if temp_count==2
bysort lbnr: egen temp_nmatched=sum(matched)
drop if temp_nmatched>0&matched==.
sort lbnr temp_diff
by lbnr: gen temp_order2=_n
drop if temp_nmatched>1&temp_order2>1
bysort id: gen temp_count2=_N
replace matched=. if temp_count2==1
drop temp*

sort matched matchinggroup pscore
by matched matchinggroup: replace id=id[_n-1] if target_lead[_n-1]==1&nmerger==0&id==.&pscore[_n-1]!=.&pscore!=.&matched==.
by matched matchinggroup: replace id=id[_n+1] if target_lead[_n+1]==1&nmerger==0&id==.&pscore[_n+1]!=.&pscore!=.&matched==.
bysort id: egen temp_pscore=max(target_lead*pscore)
gen temp_diff=(pscore-temp_pscore)^2
sort id temp_diff
by id: gen temp_order=_n
replace id=. if temp_order>2
bysort id: gen temp_count=_N
replace matched=1 if temp_count==2
bysort lbnr: egen temp_nmatched=sum(matched)
drop if temp_nmatched>0&matched==.
sort lbnr temp_diff
by lbnr: gen temp_order2=_n
drop if temp_nmatched>1&temp_order2>1
bysort id: gen temp_count2=_N
replace matched=. if temp_count2==1
drop temp*

sort matched matchinggroup pscore2
by matched matchinggroup: replace id=id[_n-1] if target_lead[_n-1]==1&nmerger==0&id==.&pscore2[_n-1]!=.&pscore2!=.&matched==.
by matched matchinggroup: replace id=id[_n+1] if target_lead[_n+1]==1&nmerger==0&id==.&pscore2[_n+1]!=.&pscore2!=.&matched==.
bysort id: egen temp_pscore2=max(target_lead*pscore2)
gen temp_diff=(pscore2-temp_pscore2)^2
sort id temp_diff
by id: gen temp_order=_n
replace id=. if temp_order>2
bysort id: gen temp_count=_N
replace matched=1 if temp_count==2
bysort lbnr: egen temp_nmatched=sum(matched)
drop if temp_nmatched>0&matched==.
sort lbnr temp_diff
by lbnr: gen temp_order2=_n
drop if temp_nmatched>1&temp_order2>1
bysort id: gen temp_count2=_N
replace matched=. if temp_count2==1
drop temp*

keep if matched==1

cap drop target_year
bysort id: egen target_year=max(target_lead*year)
replace target_year=target_year+1

save "$project\matching_2018", replace

sum ln_hrwage log_income fe* vapp log_nworker emp_growth wage_growth sh_educ* sh_female avg_age avg_exper nestab firmage if target_lead==1,d
sum ln_hrwage log_income fe* vapp log_nworker emp_growth wage_growth sh_educ* sh_female avg_age avg_exper nestab firmage if target_lead==0,d


use "$home\persondata_v3.dta", clear

*tsset pnr year
*tsfill
*gen byte fill=(lbnr==.)
*append using "I:\Workdata\702525\Alex\ind\ind_notmatched"

/* Rename variables */
rename akasst year_uifund
rename arblhu ui
ren koen sex
ren IE_TYPE immigrant
ren arledgr frac_unemployed
ren brutto gross_income
ren perindkp total_income
ren civst married
ren tlonkval quality_hour
ren aldernov age
ren koejd housing
gen ln_hrwage=log(timelon)
gen log_income=log(lonind)
gen max_educ=int(hffsp/1000000)
gen educ=1 if max_educ<=25
replace educ=2 if max_educ==35
replace educ=3 if max_educ>=40&max_educ<=50
replace educ=4 if max_educ>=60&max_educ<.
gen byte female=(sex==2)
destring ansaar, replace
gen tenure=year-ansaar
replace tenure=. if tenure>exper
bysort pnr lbnr: egen temp_firstyear=min(year)
by pnr: egen temp_firstyearever=min(year)
replace tenure=year-temp_firstyear if tenure==.&temp_firstyear!=temp_firstyearever
drop temp*
drop ansaar

*worker relative rank
sort lbnr year log_income
by lbnr year: gen rank=_n
by lbnr year: gen nworker=_N
replace rank=rank/nworker

/* Get industry info */
gen pb=persbrc
merge m:1 pb year using "$home\industrycodes\crosswalk_pb07", nogen keep(1 3)
replace pb07=persbrc if pb07==.
merge m:1 pb07 using "$home\industrycodes\ind36", nogen keep(1 3)
merge m:1 pb07 using "$home\industrycodes\ind127", nogen keep(1 3)
replace ind36="X" if ind36==""
replace ind127=99999 if ind127==.
drop branche persbrc pb

merge m:1 lbnr year using "$project\matching_2018", keepus(matched target_year target_lead id) keep(1 3) nogen

replace matched=0 if ind36=="O"|ind36=="P"|ind36=="QA"|ind36=="R"|ind36=="QB"
replace id=. if ind36=="O"|ind36=="P"|ind36=="QA"|ind36=="R"|ind36=="QB"

*drop if no control group
*bysort id: egen nfirm_id=nvals(lbnr)
*replace matched=0 if nfirm_id!=2

replace matched=0 if matched==.
bysort pnr: egen nmatched=sum(matched)
bysort lbnr: egen matched_firm=max(matched)
keep if nmatched==1|matched_firm==1

ren target_year temp
bysort lbnr: egen target_year=max(temp*matched)
bysort lbnr: egen treated_firm=max(target_lead*matched)
bysort pnr: egen temp2=max(temp*matched)
replace target_year=temp2 if target_year==.|target_year==0
gen tau=year-target_year
bysort pnr: egen treated=max(target_lead*matched)
replace treated=1 if treated_firm==1&nmatched==0
replace treated=0 if treated==.

forv i=5(-1)2 {
gen byte taub`i'=(tau==-`i')
gen byte taub`i'_t=(tau==-`i'&treated==1)
}

forv i=0/5 {
gen byte tau`i'=(tau==`i')
gen byte tau`i'_t=(tau==`i'&treated==1)
}

bysort pnr: egen mintau=min(tau)
bysort pnr: egen maxtau=max(tau*(lbnr!=.))
bysort pnr: egen age_event=max(matched*age)
bysort pnr: egen tenure_event=max(matched*tenure)
egen branche=group(ind36)
gen exper_educ1=exper*(educ==1)
gen exper_educ2=exper*(educ==2)
gen exper_female=exper*female
replace age_event=age-tau-1 if nmatched==0

/*
gen age_group=1 if (age_event<=25)
replace age_group=2 if (age_event>25&age_event<40)
replace age_group=3 if (age_event>=40&age_event<50)
replace age_group=4 if (age_event>=50)

*replace lonind=0 if fill==1&tau>=0&tau!=.
gen byte highrank=(pstill<33)
gen byte manager=(pstill==31)
*/

*heterogeneity
merge m:1 isco88 using "$project\occupation\occupation", keep(1 3) keepus(rt1 men1 man1) nogen
xtile routine=rt1, n(3)
replace routine=0 if routine<=2
replace routine=1 if routine==3

gen fcsl=0
replace fcsl=1 if isco88>=5120&isco88<=5123
replace fcsl=1 if isco88>=7140&isco88<=7143
replace fcsl=1 if isco88==9132|isco88==9141|isco88==9142
replace fcsl=1 if isco88==9151|isco88==9152
replace fcsl=1 if isco88==8322|isco88==8323|isco88==8324

*firm FE
merge m:1 lbnr year using "$project\merger_lbnr", keepus(lbnr_merger target) keep(1 3) nogen 
ren lbnr_merger firmid
merge m:1 firmid using "$project\AKM\firmeffs_annualwage_ma_lbnr_separate.dta", keep(1 3) keepus(fe_1) nogen
ren fe_1 fe_lbnr

drop firmid
ren cvrnr firmid
merge m:1 firmid using "$project\AKM\firmeffs_annual_cvrnr.dta", keep(1 3) keepus(fe_1) nogen
ren firmid cvrnr
ren fe_1 fe_cvrnr

/*productivity
merge m:1 cvrnr year using "$project\productivity", keepus(tfp_ols) keep(1 3) nogen
*/

*VA
merge m:1 cvrnr year using "$home\firmdata_v1.dta", keepus (valueadded fulltimeeq sales) nogen keep(1 3)
replace ind36="SA" if ind127>=94000&ind127<=96000
replace ind36="LA" if ind127==68001|ind127==68003
replace ind36="LB" if ind127==68002
replace ind36="SB" if ind127==97000
merge m:1 ind36 year using "$home/output_ind36", keep(1 3) nogen
foreach var in valueadded sales {
replace `var'=`var'/output_current*output_chained
}
gen vapp=log(valueadded/fulltimeeq)

merge m:1 cvrnr year using "$project\dhhi2", keep(1 3) nogen
tsset pnr year
gen dhhi_lead=F.dhhi if F.lbnr==lbnr

merge m:1 lbnr year using "$project\diff_fe_vapp", keep(1 3) nogen

merge m:1 lbnr year using "$project\sameindustry", keep(1 3) keepus(sameindustry_lead) nogen
*also constructed in diff_fe_vapp.do

merge m:1 lbnr year using "$project\merger_lbnr", keep(1 3) keepus(ratio_lead) nogen

merge m:1 ind127 using "$project\hhi", nogen
replace high_hhi=high_hhi-1

merge m:1 lbnr year using "$project\merger_lbnr", keepus(muni nest) keep(1 3) nogen
merge m:1 muni year using "$home\muni_population", keep(1 3) nogen

merge m:1 cvrnr year using "$project\multifirm_manager", keep(1 3) nogen

*merge m:1 lbnr year using "$project\wageresid", keep(1 3) nogen

merge m:1 lbnr using "$project\wage_resid_new", keep(1 3) nogen

merge 1:1 pnr year using "$project\ratio", keep(1 3) nogen

merge 1:1 pnr year using "$home\archive\samlesamle1", keepus(lonsystem) keep(1 3) nogen

merge m:1 lbnr year using "$project\agency_sumshare", keep(1 3) nogen

merge m:1 cvrnr year using "$home\fire\fire", keep(1 3) keepus(virkform) nogen

bysort pnr lbnr: egen nmatched_spell=sum(matched)
bysort pnr lbnr: egen maxtau_spell=max(tau*(lbnr!=.))

save "$project\matching_analysis_new", replace



********************************************************
********************************************************
*****Analysis     ***************
********************************************************
********************************************************


use "$project\matching_analysis_new", clear
cap drop temp*

*heterogeneity by manager fe

ren pnr pnro
merge m:1 cvrnr year using "$project\manager\manager_all_cvrnr2", keep(1 3) keepus(pnr) nogen
ren pnr pnrm
merge m:1 pnrm using "$project/managerfe_1step_over5", keep(1 3) nogen
*merge m:1 pnr using "$project/python/prediction", keep(1 3) nogen
drop pnrm
ren pnro pnr
ren me_bayes pe_manager
merge m:1 cvrnr year using "$project\manager\cvrnryear_fe_new", keep(1 3) nogen

cap drop treated1 taub3_t1-tau5_t0
sum pe_manager if matched==1&treated==1,d
bysort pnr: egen treated1=max((pe_manager>-0.16)*treated*matched)
bysort pnr: egen temp=sum((pe_manager!=.)*treated*matched)
replace treated1=. if temp==0 & treated==1
replace treated1=0 if treated==0
drop temp*

forv i=3(-1)2 {
gen byte taub`i'_t1=(tau==-`i')*treated1*treated
}

forv i=0/5 {
gen byte tau`i'_t1=(tau==`i')*treated1*treated
}

forv i=3(-1)2 {
gen byte taub`i'_t0=(tau==-`i')*(1-treated1)*treated
}

forv i=0/5 {
gen byte tau`i'_t0=(tau==`i')*(1-treated1)*treated
}

eststo clear

/*BASELINE ESTIMATION (Figure 3) */

eststo: reghdfe log_income taub3-tau5_t exper_educ1-exper_fem if nmatched==1&tau>=-3&tau<=5&age_event>20&age_event<60, absorb(pnr) vce(cluster lbnr)

eststo: reghdfe log_income taub3-tau5_t exper_educ1-exper_fem if nmatched==1&tau>=-3&tau<=5&age_event>20&age_event<60&nmatched_s==1, absorb(pnr#lbnr) vce(cluster lbnr)

eststo: reghdfe log_income taub3 taub2 tau0 tau1 tau2 tau3 tau4 tau5 taub3_t1-tau5_t0 exper_educ1-exper_fem if tau>=-3&tau<=5&age_event>20&age_event<60 & nmatched==1, absorb(pnr) vce(cluster lbnr)

eststo: reghdfe log_income taub3 taub2 tau0 tau1 tau2 tau3 tau4 tau5 taub3_t1-tau5_t0 exper_educ1-exper_fem if tau>=-3&tau<=5&age_event>20&age_event<60 & nmatched==1&nmatched_s==1, absorb(pnr#lbnr) vce(cluster lbnr)

estout using "$result\bymanagerfe.xls", cells(b(fmt(4)) se(par fmt(4)))  legend stats(N, labels("No. of obs.")) varwidth(46) style(esttab) replace


*Robustness: by difference between manager FE

sort pnr lbnr year
by pnr lbnr: gen d_pe_manager=pe_manager[_n+1]-pe_manager if year[_n+1]==year+1
sum d_pe_manager if matched==1&treated==1,d
drop treated1 taub3_t1-tau5_t0

bysort pnr: egen treated1=max((d_pe_manager>0)*treated*matched)
bysort pnr: egen temp=sum((d_pe_manager!=.)*treated*matched)
replace treated1=. if temp==0 & treated==1
replace treated1=0 if treated==0
drop temp*

forv i=3(-1)2 {
gen byte taub`i'_t1=(tau==-`i')*treated1*treated
}

forv i=0/5 {
gen byte tau`i'_t1=(tau==`i')*treated1*treated
}

forv i=3(-1)2 {
gen byte taub`i'_t0=(tau==-`i')*(1-treated1)*treated
}

forv i=0/5 {
gen byte tau`i'_t0=(tau==`i')*(1-treated1)*treated
}

eststo clear

eststo: reghdfe log_income taub3 taub2 tau0 tau1 tau2 tau3 tau4 tau5 taub3_t1-tau5_t0 exper_educ1-exper_fem if tau>=-3&tau<=5&age_event>20&age_event<60 & nmatched==1, absorb(pnr) vce(cluster lbnr)

eststo: reghdfe log_income taub3 taub2 tau0 tau1 tau2 tau3 tau4 tau5 taub3_t1-tau5_t0 exper_educ1-exper_fem if tau>=-3&tau<=5&age_event>20&age_event<60 & nmatched==1&nmatched_s==1, absorb(pnr#lbnr) vce(cluster lbnr)

estout using "$result\bymanagerfe2.xls", cells(b(fmt(4)) se(par fmt(4)))  legend stats(N, labels("No. of obs.")) varwidth(46) style(esttab) replace



*heterogeneity by worker skill level

bysort pnr: egen log_income0=sum(log_income*(tau==-1))
xtile q_income = log_income0, n(2)

eststo clear

forv i=1/2 {

reghdfe log_income taub3-tau5_t exper_educ1-exper_fem if nmatched==1&tau>=-3&tau<=5&/*
*/age_event>20&age_event<60&q_income==`i', absorb(i.pnr) vce(cluster lbnr)

reghdfe log_income taub3-tau5_t exper_educ1-exper_fem if nmatched==1&tau>=-3&tau<=5&age_event>20/*
*/&age_event<60&nmatched_s==1&q_income==`i', absorb(i.pnr#i.lbnr) vce(cluster lbnr)

}


*robustness

cap drop treated1 taub3_t1-tau5_t0
sum pe_manager if matched==1&treated==1,d
bysort pnr: egen treated1=max((pe_manager>-0.16)*treated*matched)
bysort pnr: egen temp=sum((pe_manager!=.)*treated*matched)
replace treated1=. if temp==0 & treated==1
replace treated1=0 if treated==0
drop temp*

forv i=3(-1)2 {
gen byte taub`i'_t1=(tau==-`i')*treated1*treated
}

forv i=0/5 {
gen byte tau`i'_t1=(tau==`i')*treated1*treated
}

forv i=3(-1)2 {
gen byte taub`i'_t0=(tau==-`i')*(1-treated1)*treated
}

forv i=0/5 {
gen byte tau`i'_t0=(tau==`i')*(1-treated1)*treated
}

gen post=(tau>0)
gen post_t1=(tau>0)*treated1*treated
gen post_t0=(tau>0)*(1-treated1)*treated
gen post_t=post_t1+post_t0

egen ind36_num=group(ind36)
drop ind36

gen logvapp=log(valueadded/nworker)

bysort pnr: egen nworkermatched=sum(nworker*matched)

eststo clear

eststo: reghdfe log_income post post_t exper_educ1-exper_fem if tau>=-3&tau<=5&age_event>20&age_event<60 & nmatched==1&nmatched_s==1, absorb(pnr#lbnr) vce(cluster lbnr)

eststo: reghdfe log_income post post_t exper_educ1-exper_fem if tau>=-3&tau<=5&age_event>20&age_event<60 & nmatched==1&nmatched_s==1, absorb(pnr#lbnr i.ind36#i.year) vce(cluster lbnr)

eststo: reghdfe log_income post post_t exper_educ1-exper_fem if tau>=-3&tau<=5&age_event>20&age_event<60 & nmatched==1&nmatched_s==1, absorb(pnr#lbnr i.ind36#i.year i.isco#i.year) vce(cluster lbnr)

eststo: reghdfe log_income post post_t exper_educ1-exper_fem tau if tau>=-3&tau<=5&age_event>20&age_event<60 & nmatched==1&nmatched_s==1, absorb(pnr#lbnr) vce(cluster lbnr)

eststo: reghdfe log_income post post_t exper_educ1-exper_fem logvapp if tau>=-3&tau<=5&age_event>20&age_event<60 & nmatched==1&nmatched_s==1, absorb(pnr#lbnr) vce(cluster lbnr)

eststo: reghdfe ln_hrwage post post_t exper_educ1-exper_fem if tau>=-3&tau<=5&age_event>20&age_event<60 & nmatched==1&nmatched_s==1, absorb(pnr#lbnr) vce(cluster lbnr)

estout using "$result\eventstudy.xls", cells(b(fmt(4)) se(par fmt(4)))  legend stats(N, labels("No. of obs.")) varwidth(46) style(esttab) replace


/* SELECTION: BOUNDING METHOD BY LEE */

tsset pnr year
gen d_wage=.
gen byte stay=0
forv i=1/6 {
replace d_wage=log_income-L`i'.log_income if tau==`i'-1
replace stay=1 if lbnr==L`i'.lbnr & tau==`i'-1
}

forv i=0/5 {
reg d_wage treated if stay==1&nmatched==1&age_event>20&age_event<60&tau==`i'/*
*/&nmatched_s==1, cl(lbnr)
}

cap drop upperbd lowerbd
gen upperbd=0
gen lowerbd=0
forv i=0/5 {
count if tau==`i'&treated==1&stay==1&nmatched==1&age_event>20&age_event<60
global STAYTREATED=r(N)
count if tau==`i'&treated==1&nmatched==1&age_event>20&age_event<60
global TREATED=r(N)
count if tau==`i'&treated==0&nmatched==1&stay==1&age_event>20&age_event<60
global STAYCONTROL=r(N)
count if tau==`i'&treated==0&nmatched==1&age_event>20&age_event<60
global CONTROL=r(N)
global ratio=1*($STAYCONTROL /$CONTROL -$STAYTREATED /$TREATED )/(1-$STAYTREATED /$TREATED )
_pctile d_wage if tau==`i'&treated==1&stay==0&nmatched==1&age_event>20&age_event<60, p($ratio)
replace lowerbd=1 if tau==`i'&treated==1&stay==0&nmatched==1&age_event>20&age_event<60&d_wage<=r(r1)
global ratioo=100-$ratio
_pctile d_wage if tau==`i'&treated==1&stay==0&nmatched==1&age_event>20&age_event<60, p($ratioo )
replace upperbd=1 if tau==`i'&treated==1&stay==0&nmatched==1&d_wage>=r(r1)&age_event>20&age_event<60
}

eststo clear

forv i=0/5 {
eststo: reg d_wage treated if (stay==1|upperbd==1)&nmatched==1&age_event>20&age_event<60&tau==`i', cl(lbnr)
}

forv i=0/5 {
eststo: reg d_wage treated if (stay==1|lowerbd==1)&nmatched==1&age_event>20&age_event<60&tau==`i', cl(lbnr)
}

cap drop upperbdd lowerbdd
gen upperbdd=0
gen lowerbdd=0
forv i=0/5 {
count if tau==`i'&treated==1&stay==1&nmatched==1&age_event>20&age_event<60
global STAYTREATED=r(N)
count if tau==`i'&treated==1&nmatched==1&age_event>20&age_event<60
global TREATED=r(N)
count if tau==`i'&treated==0&nmatched==1&stay==1&age_event>20&age_event<60
global STAYCONTROL=r(N)
count if tau==`i'&treated==0&nmatched==1&age_event>20&age_event<60
global CONTROL=r(N)
global ratio=5*($STAYCONTROL /$CONTROL -$STAYTREATED /$TREATED )/($STAYCONTROL /$CONTROL )
_pctile d_wage if tau==`i'&treated==0&stay==1&nmatched==1&age_event>20&age_event<60, p($ratio)
replace upperbdd=1 if tau==`i'&treated==0&stay==1&nmatched==1&d_wage>=r(r1)&age_event>20&age_event<60
global ratioo=100-$ratio
_pctile d_wage if tau==`i'&treated==0&stay==1&nmatched==1&age_event>20&age_event<60, p($ratioo )
replace lowerbdd=1 if tau==`i'&treated==0&stay==1&nmatched==1&d_wage<=r(r1)&age_event>20&age_event<60
}

forv i=0/5 {
reg d_wage treated if stay==1&nmatched==1&age_event>20&age_event<60&tau==`i'&(treated==1|lowerbdd==1), cl(lbnr)
}

eststo clear

eststo: reghdfe log_income taub3-tau5_t exper_educ1-exper_fem if nmatched==1&tau>=-3&tau<=5&age_event>20/*
*/&age_event<60&nmatched_s==1&(treated==1|lowerbdd==1|tau<0)&nworkermatched>11, absorb(i.pnr) vce(cluster lbnr)

eststo: reghdfe log_income taub3-tau5_t exper_educ1-exper_fem if nmatched==1&tau>=-3&tau<=5&age_event>20/*
*/&age_event<60&nmatched_s==1&(treated==1|upperbdd==1|tau<0)&nworkermatched>11, absorb(i.pnr) vce(cluster lbnr)

estout using "$result\bounding.xls", cells(b(fmt(4)) se(par fmt(4)))  legend stats(N, labels("No. of obs."))  varwidth(46) style(esttab) replace
